<!--
 * @Description: 赠送云/酒币弹框
-->
<template>
  <el-dialog :title="`赠送${type == 0 ? '酒币' : '云币'}`" :visible.sync="dialogVisible" width="30%" @open="open"
    @closed="handleClose" @close="onClose">
    <el-form :model="ruleForm" :rules="rules" ref="ruleFormRef" label-width="80px">
      <el-form-item label="赠送数量" prop="number">
        <el-input-number v-model="ruleForm.number" :min="0" :step="1" step-strictly style="width: 100%" />
      </el-form-item>
      <el-form-item v-if="type == 1" label="云币类型" prop="isDirectedExchange">
        <el-radio-group v-model="ruleForm.isDirectedExchange">
          <el-radio :label="0">普通云币</el-radio>
          <el-radio :label="1">定向云币</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="赠送原因" prop="cause">
        <el-input type="textarea" v-model="ruleForm.cause" :autosize="{ minRows: 2 }" />
      </el-form-item>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="submitForm('ruleFormRef')" :loading="loading">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { addIntegral } from '@/api/memberinfo';
export default {
  props: {
    show: { type: Boolean, default: false },
    // 会员id
    memberId: { type: String, default: '' },
    // 类型 0会员赠送云币 / 1商家赠送酒币
    type: { type: [String, Number], default: 0 },
    // 门店组织机构编码
    orgSeq: { type: String, default: '' },
  },
  data() {
    return {
      loginInfo: localStorage.getItem('logininfo'),
      ruleForm: {
        memberId: '',
        orgSeq: '',
        number: '',
        cause: '',
        // 类型：0会员赠送云币 / 1商家赠送酒币
        type: 0,
        isDirectedExchange: 0,
      },
      loading: false
    };
  },
  computed: {
    dialogVisible: {
      get() {
        return this.show;
      },
      set(val) {
        this.$emit('update:show', val);
      },
    },
    rules: function () {
      const defaultRules = {
        number: [
          { type: 'number', required: true, message: '请输入赠送数量', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              if (value <= 0) {
                callback(new Error('赠送数量不能小于0'));
              } else {
                callback();
              }
            },
            trigger: 'blur'
          }
        ],
        cause: [{ required: true, message: '请输入赠送原因', trigger: 'blur' }],
      };
      return this.type == 1 ? { ...defaultRules, isDirectedExchange: [{ required: true, message: '请选择云币类型', trigger: 'change' }] } : { ...defaultRules };
    },
  },
  created() { },
  methods: {
    // 弹框打开
    open() {
      const orgSeq = this.orgSeq || JSON.parse(this.loginInfo).orgSeq;
      this.ruleForm.orgSeq = orgSeq;
      this.ruleForm.type = this.type;
      this.ruleForm.memberId = this.memberId;
    },

    // 关闭前的回调
    handleClose() {
      this.$refs.ruleFormRef.resetFields();
    },

    onClose() {
      this.ruleForm.memberId = '';
    },

    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true;
          const data = {
            ...this.ruleForm
          }
          addIntegral(data).then((res) => {
            if (res.code == 1) {
              this.$message({
                message: res.message,
                type: 'success',
              });
              this.dialogVisible = false;
              this.$emit('refresh');
            } else {
              this.$message({
                message: res.message,
                type: 'error',
              });
            }
          }).finally(() => {
            this.loading = false;
          })
        }
      });
    },
  },
};
</script>
